package com.algorithm.deleteBottomList;

import com.algorithm.mergeTwolinked.Node;

public class DeleteBottomList {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        Node head = create(arr);
        deleteBottomList(head, 3);
        print(head);
    }

    public static com.algorithm.mergeTwolinked.Node create(int[] arr) {
        com.algorithm.mergeTwolinked.Node head = new com.algorithm.mergeTwolinked.Node(arr[0], null);
        com.algorithm.mergeTwolinked.Node p = head;
        for (int i = 1; i < arr.length; i++) {
            com.algorithm.mergeTwolinked.Node node = new Node(arr[i], null);
            p.setNext(node);
            p = node;
        }
        return head;
    }

    public static void print(Node head) {
        Node p = head;
        while (p != null) {
            System.out.print(p.getData() + " ");
            p = p.getNext();
        }
        System.out.println();
    }

    public static void deleteBottomList(Node head, int k) {
        Node fast = head;
        for (int i = 0; i < k; i++) {
            fast = fast.getNext();
        }
        Node slow = head;
        while(fast.getNext() != null){
            fast = fast.getNext();
            slow = slow.getNext();
        }
        slow.setNext(slow.getNext().getNext());
    }
}
